
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait


# 放一些基础操作的方法
class  BasePage:
    def __init__(self,driver):
        self.__driver = driver
        # 创建点击对象
        self.__action = ActionChains(self.__driver)

    # 查找元素
    def find_page_element(self,locator:tuple)->WebElement:
        # 给定位添加显示等待
        return WebDriverWait(self.__driver,15).until(lambda x:x.find_element(*locator))

    def find_page_element1(self,locator,path="CSS_SELECTOR")->WebElement:
        # 动态获取 By 类的属性 从对象获取命名属性;
        # getattr（x， 'y'） 等效于x.y 当给出default参数时，当attribute没有存在;如果没有它，在这种情况下会引发异常。
        by = getattr(By, path.upper())
        # 给定位添加显示等待
        return WebDriverWait(self.__driver,10).until(lambda x:x.find_element(by,locator))

    # 输入框输入内容
    # locator元素定位，value输入框内容
    def input_page_element(self,locator:tuple,value:str):
        self.find_page_element(locator).send_keys(value)

    # 点击登录按钮
    def click_login_button(self,locator):
        self.__driver.find_element(*locator).click()

    # 获取弹窗的信息
    def get_page_element_text(self,locator):
        return self.find_page_element(locator).text

    # 模拟鼠标点击
    def click_mouse(self,locator):
        self.__action.click(self.__driver.find_element(*locator)).perform()

# if __name__ == '__main__':
#     from selenium import webdriver
#     driver = webdriver.Chrome()
#     driver.get("http://123.57.228.35/Home/user/login.html")
#     base_page = BasePage(driver)
#     # base_page.find_page_element((By.CSS_SELECTOR,"#username")).send_keys("15514091712")
#     base_page.find_page_element1("#username").send_keys("15514091712")
#     time.sleep(2)